#version 400 core
#extension GL_ARB_shading_language_420pack : enable

out vec4 color;

layout (binding = 1) uniform sampler2D tex_color;

//uniform bool enable_fog = true;
//uniform vec4 fog_color = vec4(0.7, 0.8, 0.9, 0.0);

uniform bool enable_fog;
//uniform vec4 fog_color;

in TES_OUT
{
    vec2 tc;
    vec3 world_coord;
    vec4 eye_coord;
} fs_in;

float calcExpXFog(vec4 vecPos)
{
    float fogDensity = 0.1;
    float LOG2E = 1.442695;
    float fogDist = abs(vecPos.z);
    float result = exp2( -fogDensity * fogDensity *
        fogDist * fogDist * LOG2E );
    result = clamp(result, 0.0, 1.0);
    return result;
}

void main(void)
{
    vec4 landscape = texture(tex_color, fs_in.tc);

    if (enable_fog)
    {
        vec4 fogColor = vec4(0.2f, 0.2f, 0.2f, 1.0f);
        float fogAlpha = calcExpXFog(fs_in.eye_coord);
        color = mix(fogColor, landscape, fogAlpha);
    }
    else
    {
        color = landscape;
    }
}
